package com.topisv.tms.utils.taobao;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.WaybillAddress;
import com.taobao.api.domain.WaybillApplyNewInfo;
import com.taobao.api.domain.WaybillApplySubscriptionInfo;
import com.taobao.api.domain.WaybillBranchAccount;
import com.taobao.api.request.WlbWaybillICancelRequest;
import com.taobao.api.request.WlbWaybillIGetRequest;
import com.taobao.api.request.WlbWaybillIPrintRequest;
import com.taobao.api.request.WlbWaybillIProductRequest;
import com.taobao.api.request.WlbWaybillISearchRequest;
import com.taobao.api.response.WlbWaybillICancelResponse;
import com.taobao.api.response.WlbWaybillIGetResponse;
import com.taobao.api.response.WlbWaybillIPrintResponse;
import com.taobao.api.response.WlbWaybillIProductResponse;
import com.taobao.api.response.WlbWaybillISearchResponse;
import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.entity.ShopSource;
import com.topisv.tms.entity.WarehouseAddress;
import com.topisv.tms.entity.taobao.TopWaybillNoGetDto;
import com.topisv.tms.entity.work.WorkOrder;
import com.topisv.tms.utils.InitProperties;
import com.topisv.tms.utils.SplitUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 淘宝开放平台的工具类
 *  Date          Author       Version       Description
 * ----------------------------------------------------------
 *  2014-8-28      CC            1.0           Create
 */
public class TopClientUtils {
	protected static Logger log = LoggerFactory.getLogger(TopClientUtils.class);
	/** 淘宝开放平台的地址--测试环境*/
	/*private static final String TOP_URL = "http://gw.api.tbsandbox.com/router/rest";
	
	private static final String APP_KEY = "1023009430";
	
	private static final String SECRET = "sandbox9e21c71ac911b5265f108b028";
	
	private static final String sessionKey = "6102928445ab68094a90642f586952eae5391c6f6ef30262054718218";*/
	
	/** 淘宝开放平台的地址--正式环境*/
	private static final String TOP_URL = InitProperties.get("top.api.url");
	
	private static final String APP_KEY = InitProperties.get("regAppKey");
	
	private static final String SECRET = InitProperties.get("regAppSecret");
	
	/**
	 * 淘宝开放平台search接口.
	 * @param cpCode
	 * @param sessionKey
	 * @return WlbWaybillSearchResponse
	 */
	public static WlbWaybillISearchResponse topSearchByCpCode(String cpCode, String sessionKey) throws ApiException {
		TaobaoClient client = new DefaultTaobaoClient(TOP_URL, APP_KEY, SECRET);
		WlbWaybillISearchRequest req = new WlbWaybillISearchRequest();
		JSONObject json = new JSONObject();
		json.put("cp_code", cpCode);
		req.setWaybillApplyRequest(json.toString());
		return client.execute(req, sessionKey);
	}
	/**
	 * 淘宝开放平台product接口.
	 * @param cpCode
	 * @param sessionKey
	 * @return WlbWaybillSearchResponse
	 */
	public static WlbWaybillIProductResponse topProductByCpCode(String cpCode, String sessionKey) throws ApiException {
		TaobaoClient client = new DefaultTaobaoClient(TOP_URL, APP_KEY, SECRET);
		WlbWaybillIProductRequest req = new WlbWaybillIProductRequest();
		JSONObject json = new JSONObject();
		json.put("cp_code", cpCode);
		req.setWaybillProductTypeRequest(json.toString());
		return client.execute(req, sessionKey);
	}

	/**
	 * 从淘宝开放平台，通过订单号获取电子面单号
	 * @param order
	 * @param sessionKey
	 * @return WlbWaybillGetResponse
	 */
	public static List<WlbWaybillIGetResponse> topGetWaybillNoBatch(TopWaybillNoGetDto waybillDto,Map<String,WarehouseAddress> warehouseAddressMap) throws ApiException {
		List<WlbWaybillIGetResponse> responses = new ArrayList<WlbWaybillIGetResponse>();
		Map<ShopSource, Map<String, List<WorkOrder>>> data = waybillDto.getData();
		for (Map.Entry<ShopSource, Map<String, List<WorkOrder>>> orders : data.entrySet()) {
			ShopSource shopSource = orders.getKey();
			String sessionKey = shopSource.getTopisvSessionKey();
			for (Map.Entry<String, List<WorkOrder>> entry : orders.getValue().entrySet()) {
				String companyCode = entry.getKey();// 快递公司编码例如：顺丰-SF
				List<WorkOrder> value = entry.getValue();
//				String shippingAddress = getShippingAddress(companyCode, sessionKey).toString();
//				//接口最大支持10个订单号一批
//				List<List<WorkOrder>> splitOrders = SplitUtil.split(value, 10);
//				for (List<WorkOrder> splitOrder : splitOrders) {
//					TaobaoClient client = new DefaultTaobaoClient(TOP_URL, APP_KEY, SECRET);
//					WlbWaybillIGetRequest req = new WlbWaybillIGetRequest();
//					JSONObject json = new JSONObject();
//					json.put("cp_code", companyCode);
//					json.put("shipping_address", shippingAddress);
//					json.put("trade_order_info_cols", getTradeOrderInfoCols(splitOrder, shopSource));
//					req.setWaybillApplyNewRequest(json.toString());
//					responses.add(client.execute(req, sessionKey));
//				}
				for(WorkOrder workOrder:value){
					TaobaoClient client = new DefaultTaobaoClient(TOP_URL, APP_KEY, SECRET);
					WlbWaybillIGetRequest req = new WlbWaybillIGetRequest();
					JSONObject json = new JSONObject();
					json.put("cp_code", companyCode);
					json.put("shipping_address", getWareHouseAddress(workOrder,warehouseAddressMap).toString());
					json.put("trade_order_info_cols", getTradeOrderInfoColsOne(workOrder, shopSource,warehouseAddressMap));
					log.info(workOrder.getOrder()+"获取菜鸟电子面单号xml:"+json.toString());
					req.setWaybillApplyNewRequest(json.toString());
					responses.add(client.execute(req, sessionKey));
				}
			}
			
		}
		return responses;
	}
	/**
	 * 根据仓库地址构建发货地址
	 */
	public static  JSONObject getWareHouseAddress(WorkOrder workOrder,Map<String,WarehouseAddress> warehouseAddressMap) throws ApiException {
		WarehouseAddress warehouseAddress= warehouseAddressMap.get(workOrder.getWarehouseCode());
		JSONObject json = new JSONObject();
		json.put("province", warehouseAddress.getProvName());// 省
		json.put("city", warehouseAddress.getCityName());// 市
		json.put("area", warehouseAddress.getDistName());// 区
//		json.put("town", "");// 街道
		json.put("address_detail", warehouseAddress.getAddress());// 详细地址
		return json;
	}
	
	
	/**
	 * 淘宝接口构建发货地址
	 * @param cpCode
	 * @param sessionKey
	 * @return String
	 * @throws ApiException 
	 */
	public static JSONObject getShippingAddress(String cpCode, String sessionKey) throws ApiException {
		WlbWaybillISearchResponse wlbWaybillSearchResponse = topSearchByCpCode(cpCode, sessionKey);
		if (wlbWaybillSearchResponse != null) {
			List<WaybillApplySubscriptionInfo> results = wlbWaybillSearchResponse.getSubscribtions();
			WaybillApplySubscriptionInfo waybillApplySubscriptionInfo = results.get(0);
			List<WaybillBranchAccount> branchAccountCols = waybillApplySubscriptionInfo.getBranchAccountCols();
			WaybillBranchAccount waybillBranchAccount = branchAccountCols.get(0);
			List<WaybillAddress> shippAddressCols = waybillBranchAccount.getShippAddressCols();
			WaybillAddress waybillAddress = shippAddressCols.get(0);
			JSONObject json = new JSONObject();
			json.put("province", waybillAddress.getProvince());// 省
			json.put("city", waybillAddress.getCity());// 市
			json.put("area", waybillAddress.getArea());// 区
			json.put("town", waybillAddress.getTown());// 街道
			json.put("address_detail", waybillAddress.getAddressDetail());// 详细地址
			return json;
		}
		return null;
	}
	/**
	 * 构建交易信息
	 * @param order
	 * @param sessionKey
	 * @return String
	 */
	public static JSONArray getTradeOrderInfoColsOne(WorkOrder orders, ShopSource shopSource,Map<String,WarehouseAddress> warehouseAddressMap ) {
		JSONArray tradeOrderInfoCols = new JSONArray();
		EdiOrder order = orders.getOrder();
		JSONObject tradeOrderInfoCol = new JSONObject();
		JSONObject consigneeAddress = new JSONObject();
		consigneeAddress.put("address_detail", order.getAddres());// 地址
		consigneeAddress.put("town", order.getTown());// 街道
		consigneeAddress.put("area", order.getDistna());// 区
		consigneeAddress.put("city", order.getCityna());// 市
		consigneeAddress.put("province", order.getProvna());// 省
		tradeOrderInfoCol.put("consignee_address", consigneeAddress);// 收货地址
		tradeOrderInfoCol.put("consignee_name", order.getRecena());// 收件人姓名
		tradeOrderInfoCol.put("consignee_phone", dealconsigneePhone(StringUtils.isEmpty(order.getMobile()) ? order.getTeleph() : order.getMobile()));// 联系方式
		
		//package_items
		JSONArray packages = new JSONArray();
		JSONObject packageItem = new JSONObject();
		packageItem.put("count", order.getSkuQty());
		packageItem.put("item_name", "商品名称");// 商品名称
		packages.add(packageItem);
		tradeOrderInfoCol.put("package_items", packages);
		tradeOrderInfoCol.put("product_type", "STANDARD_EXPRESS");//产品类型
		tradeOrderInfoCol.put("real_user_id", shopSource.getTopisvUserId());//面单使用者的用户帐号id
		/**begin add 20160722 增加发货人的名称和发货人的电话 zhangweize*/
		tradeOrderInfoCol.put("send_name", warehouseAddressMap.get(order.getWarehouseCode()).getContact());//发货人名称
		tradeOrderInfoCol.put("send_phone",warehouseAddressMap.get(order.getWarehouseCode()).getMobile());//发货人电话
		/**end add 20160722 增加发货人的名称和发货人的电话 zhangweize*/
		tradeOrderInfoCol.put("order_channels_type", dealChannelsType(order.getSourcePlatformCode()));// 订单来源
		//tradeOrderInfoCol.put("order_channels_type", order.getErpType());// 订单来源
		String[] orders2 = order.getOrdeno().split(";");
		Arrays.sort(orders2);
		tradeOrderInfoCol.put("trade_order_list", orders2);// 订单号
		tradeOrderInfoCol.put("package_id", order.getOrdeno());//包裹号
		tradeOrderInfoCols.add(tradeOrderInfoCol);
		return tradeOrderInfoCols;
	}
	/**
	 * 批量构建交易信息
	 * @param order
	 * @param sessionKey
	 * @return String
	 */
	public static JSONArray getTradeOrderInfoCols(List<WorkOrder> orders, ShopSource shopSource) {
		JSONArray tradeOrderInfoCols = new JSONArray();
		for (WorkOrder workorder : orders) {
			EdiOrder order = workorder.getOrder();
			JSONObject tradeOrderInfoCol = new JSONObject();
			JSONObject consigneeAddress = new JSONObject();
			consigneeAddress.put("address_detail", order.getAddres());// 地址
			consigneeAddress.put("town", order.getTown());// 街道
			consigneeAddress.put("area", order.getDistna());// 区
			consigneeAddress.put("city", order.getCityna());// 市
			consigneeAddress.put("province", order.getProvna());// 省
			tradeOrderInfoCol.put("consignee_address", consigneeAddress);// 收货地址
			tradeOrderInfoCol.put("consignee_name", order.getRecena());// 收件人姓名
			tradeOrderInfoCol.put("consignee_phone", dealconsigneePhone(StringUtils.isEmpty(order.getMobile()) ? order.getTeleph() : order.getMobile()));// 联系方式
			
			//package_items
			JSONArray packages = new JSONArray();
			JSONObject packageItem = new JSONObject();
			packageItem.put("count", order.getSkuQty());
			packageItem.put("item_name", "商品名称");// 商品名称
			packages.add(packageItem);
			tradeOrderInfoCol.put("package_items", packages);
			tradeOrderInfoCol.put("product_type", "STANDARD_EXPRESS");//产品类型
			tradeOrderInfoCol.put("real_user_id", shopSource.getTopisvUserId());//面单使用者的用户帐号id
			//tradeOrderInfoCol.put("send_name", "");//发货人名称
			//tradeOrderInfoCol.put("send_phone", "");//发货人电话
			tradeOrderInfoCol.put("order_channels_type", dealChannelsType(order.getSourcePlatformCode()));// 订单来源
			//tradeOrderInfoCol.put("order_channels_type", order.getErpType());// 订单来源
			String[] orders2 = order.getOrdeno().split(";");
			Arrays.sort(orders2);
			tradeOrderInfoCol.put("trade_order_list", orders2);// 订单号
			tradeOrderInfoCol.put("package_id", order.getOrdeno());//包裹号
			tradeOrderInfoCols.add(tradeOrderInfoCol);
		}
		return tradeOrderInfoCols;
	}
	
	private static String dealChannelsType(String orderChannelsType){
		if(StringUtils.isNotBlank(orderChannelsType)){
			if(orderChannelsType.equals("OTHER") || "NULL".equals(orderChannelsType.toUpperCase())){
				return "OTHERS";
			}else{
				return orderChannelsType;
			}
		}else{
			return "OTHERS";
		}
//		return StringUtils.isBlank(orderChannelsType) ? "OTHERS" : orderChannelsType;
	}
	
	public static String getWaybillNo(WlbWaybillIGetResponse wlbWaybillGetResponse){
		if(StringUtils.isNotBlank(wlbWaybillGetResponse.getErrorCode())){
			throw new RuntimeException("topGetWaybillNoBatch case some error:"+wlbWaybillGetResponse.getMsg());
		}
		List<WaybillApplyNewInfo> results = wlbWaybillGetResponse.getWaybillApplyNewCols();
		if(!results.isEmpty()){
			for (WaybillApplyNewInfo waybillApplyNewInfo : results) {
				return waybillApplyNewInfo.getWaybillCode();
			}
		}
		return null;
	}
	public static String getOrderNO(WlbWaybillIGetResponse wlbWaybillGetResponse){
		if(StringUtils.isNotBlank(wlbWaybillGetResponse.getErrorCode())){
			throw new RuntimeException("topGetWaybillNoBatch case some error:"+wlbWaybillGetResponse.getMsg());
		}
		List<WaybillApplyNewInfo> results = wlbWaybillGetResponse.getWaybillApplyNewCols();
		if(!results.isEmpty()){
			StringBuilder sb = new StringBuilder();
			for (WaybillApplyNewInfo waybillApplyNewInfo : results) {
				List<String> tradeOrderList = waybillApplyNewInfo.getTradeOrderInfo().getTradeOrderList();
				Collections.sort(tradeOrderList);
				for (String string : tradeOrderList) {
					sb.append(string).append(";");
				}
				sb.deleteCharAt(sb.length()-1).toString();
			}
		}
		return null;
	}
	/**
	 * 
	 * @param waybillDto
	 * @param warehouseAddressMap
	 * @return
	 * @throws ApiException
	 */
	public static List<WlbWaybillIPrintResponse> printCheckNew(TopWaybillNoGetDto waybillDto,Map<String,WarehouseAddress> warehouseAddressMap) throws ApiException{
		List<WlbWaybillIPrintResponse> responses = new ArrayList<WlbWaybillIPrintResponse>();
		Map<ShopSource, Map<String, List<WorkOrder>>> data = waybillDto.getData();
		for (Map.Entry<ShopSource, Map<String, List<WorkOrder>>> orders : data.entrySet()) {
			ShopSource shopSource = orders.getKey();
			String sessionKey = shopSource.getTopisvSessionKey();
			for (Map.Entry<String, List<WorkOrder>> entry : orders.getValue().entrySet()) {
				String companyCode = entry.getKey();// 快递公司编码例如：顺丰-SF
				List<WorkOrder> value = entry.getValue();
				for(WorkOrder workOrder:value){
					TaobaoClient client = new DefaultTaobaoClient(TOP_URL, APP_KEY, SECRET);
					WlbWaybillIPrintRequest printRequest = new WlbWaybillIPrintRequest();
					String shippingAddress=getWareHouseAddress(workOrder,warehouseAddressMap).toString();
					JSONObject json = new JSONObject();
					json.put("cp_code", companyCode);
					json.put("print_check_info_cols", getPrintCheckInfoColsOne(workOrder, shopSource,shippingAddress,warehouseAddressMap));
					log.info(workOrder.getOrder()+"校验打印xml:"+json.toString());
					printRequest.setWaybillApplyPrintCheckRequest(json.toString());
					responses.add(client.execute(printRequest,sessionKey));
				}
			}
		}
		return responses;
	}
	/**
	 * 菜鸟接口打印校验接口
	 * @param waybillDto
	 * @throws ApiException
	 */
	public static List<WlbWaybillIPrintResponse> printCheck(TopWaybillNoGetDto waybillDto) throws ApiException{
		List<WlbWaybillIPrintResponse> responses = new ArrayList<WlbWaybillIPrintResponse>();
		Map<ShopSource, Map<String, List<WorkOrder>>> data = waybillDto.getData();
		for (Map.Entry<ShopSource, Map<String, List<WorkOrder>>> orders : data.entrySet()) {
			ShopSource shopSource = orders.getKey();
			String sessionKey = shopSource.getTopisvSessionKey();
			for (Map.Entry<String, List<WorkOrder>> entry : orders.getValue().entrySet()) {
				String companyCode = entry.getKey();// 快递公司编码例如：顺丰-SF
				List<WorkOrder> value = entry.getValue();
				String shippingAddress = getShippingAddress(companyCode, sessionKey).toString();
				// 接口最大支持10个订单号一批
				List<List<WorkOrder>> splitOrders = SplitUtil.split(value, 10);
				for (List<WorkOrder> splitOrder : splitOrders) {
					TaobaoClient client = new DefaultTaobaoClient(TOP_URL, APP_KEY, SECRET);
					WlbWaybillIPrintRequest printRequest = new WlbWaybillIPrintRequest();
					JSONObject json = new JSONObject();
					json.put("cp_code", companyCode);
					json.put("print_check_info_cols", getPrintCheckInfoCols(splitOrder, shopSource,shippingAddress));
					printRequest.setWaybillApplyPrintCheckRequest(json.toString());
					responses.add(client.execute(printRequest,sessionKey));
				}
			}
		}
		return responses;
	}
	/**
	 * 已申请过的电子面单号，可以使用该接口进行取消。
	 * 取消后的面单数量将返回给用户，但不是及时的
	 */
	public static WlbWaybillICancelResponse cancelWaybillNo(EdiOrder order,ShopSource shop) throws ApiException{
		TaobaoClient client=new DefaultTaobaoClient(TOP_URL, APP_KEY, SECRET);
		WlbWaybillICancelRequest req=new WlbWaybillICancelRequest();
		req.setWaybillApplyCancelRequest(bulidCancelRequest(order.getCacode(),shop.getTopisvUserId(),order.getOrdeno(),order.getMailNo(), order.getOrdeno()).toString());
		return client.execute(req , shop.getTopisvSessionKey());
	}
	
	public static JSONObject bulidCancelRequest(String cpCode,String topisvUserId,String ordersList,String waybillNo,String packageId){
		JSONObject request = new JSONObject();
		request.put("cp_code", cpCode);
		request.put("real_user_id", topisvUserId);
		request.put("waybill_code", waybillNo);
		String[] orders = ordersList.split(";");
		Arrays.sort(orders);
		request.put("trade_order_list", orders);
		request.put("package_id", packageId);
		return request;
	}
	public static JSONArray getPrintCheckInfoColsOne(WorkOrder workOrder,ShopSource shopSource,String shippingAddress,Map<String,WarehouseAddress> warehouseAddressMap) throws ApiException{
		JSONArray json = new JSONArray();
		EdiOrder order = workOrder.getOrder();
		JSONObject tradeOrderInfoCol = new JSONObject();
		JSONObject consigneeAddress = new JSONObject();
		consigneeAddress.put("address_detail", order.getAddres());// 地址
		consigneeAddress.put("town", order.getTown());// 镇
		consigneeAddress.put("area", order.getDistna());// 区
		consigneeAddress.put("city", order.getCityna());// 市
		consigneeAddress.put("province", order.getProvna());// 省
		tradeOrderInfoCol.put("consignee_address", consigneeAddress);// 收货地址
		tradeOrderInfoCol.put("consignee_name", order.getRecena());// 收件人姓名
		tradeOrderInfoCol.put("consignee_phone", dealconsigneePhone(StringUtils.isEmpty(order.getMobile()) ? order.getTeleph() : order.getMobile()));// 联系方式
		
		tradeOrderInfoCol.put("short_address", order.getToShortAddress());//大头笔
		tradeOrderInfoCol.put("product_type", "STANDARD_EXPRESS");//产品类型
		tradeOrderInfoCol.put("real_user_id", shopSource.getTopisvUserId());//面单使用者的用户帐号id
		/**begin add 20160722 增加发货人的名称和发货人的电话 zhangweize*/
		tradeOrderInfoCol.put("send_name", warehouseAddressMap.get(order.getWarehouseCode()).getContact());//发货人名称
		tradeOrderInfoCol.put("send_phone",warehouseAddressMap.get(order.getWarehouseCode()).getMobile());//发货人电话
		/**end add 20160722 增加发货人的名称和发货人的电话 zhangweize*/
		tradeOrderInfoCol.put("shipping_address", shippingAddress);//发货人电话
		tradeOrderInfoCol.put("waybill_code", order.getMailNo());
		json.add(tradeOrderInfoCol);
		return json;
	}
	public static JSONArray getPrintCheckInfoCols(List<WorkOrder> orders,ShopSource shopSource,String shippingAddress) throws ApiException{
		JSONArray json = new JSONArray();
		for (WorkOrder workOrder : orders) {
			EdiOrder order = workOrder.getOrder();
			JSONObject tradeOrderInfoCol = new JSONObject();
			JSONObject consigneeAddress = new JSONObject();
			consigneeAddress.put("address_detail", order.getAddres());// 地址
			consigneeAddress.put("town", order.getTown());// 镇
			consigneeAddress.put("area", order.getDistna());// 区
			consigneeAddress.put("city", order.getCityna());// 市
			consigneeAddress.put("province", order.getProvna());// 省
			tradeOrderInfoCol.put("consignee_address", consigneeAddress);// 收货地址
			tradeOrderInfoCol.put("consignee_name", order.getRecena());// 收件人姓名
			tradeOrderInfoCol.put("consignee_phone", dealconsigneePhone(StringUtils.isEmpty(order.getMobile()) ? order.getTeleph() : order.getMobile()));// 联系方式
			
			tradeOrderInfoCol.put("short_address", order.getToShortAddress());//大头笔
			tradeOrderInfoCol.put("product_type", "STANDARD_EXPRESS");//产品类型
			tradeOrderInfoCol.put("real_user_id", shopSource.getTopisvUserId());//面单使用者的用户帐号id
			//tradeOrderInfoCol.put("send_name", "");//发货人名称
			//tradeOrderInfoCol.put("send_phone", "");//发货人电话
			tradeOrderInfoCol.put("shipping_address", shippingAddress);//发货人电话
			tradeOrderInfoCol.put("waybill_code", order.getMailNo());
			json.add(tradeOrderInfoCol);
		}
		return json;
	}
	
	public static String dealconsigneePhone(String phone){
		if(!StringUtils.isBlank(phone) && phone.length() > 20){
			return phone.substring(0, 20);
		}
		return phone;
	}
	
	
	public static WorkOrder getEdiOrder(TopWaybillNoGetDto waybillDto,String orderNo){
		Map<ShopSource, Map<String, List<WorkOrder>>> data = waybillDto.getData();
		for (Map.Entry<ShopSource, Map<String, List<WorkOrder>>> orders : data.entrySet()) {
			Map<String, List<WorkOrder>> values = orders.getValue();
			for (List<WorkOrder> order : values.values()) {
				for (WorkOrder workOrder : order) {
					String ordernos =  workOrder.getOrder().getOrdeno();
					if(ordernos.equals(orderNo)){
						return workOrder;
					}
				}
			}
		}
		return null;
	}
	
	public static void main(String[] args) throws ApiException {
		/*String sessionKey = "6102928445ab68094a90642f586952eae5391c6f6ef30262054718218";
		WlbWaybillIProductResponse topProductByCpCode = topProductByCpCode("YUNDA", sessionKey);
		System.out.println(topProductByCpCode.getBody());
		WlbWaybillISearchResponse topSearchBCpCode = topSearchByCpCode("YUNDA", sessionKey);
		System.out.println(topSearchBCpCode.getBody());
		EdiOrder order = new EdiOrder();
		order.setAddres("老君镇1村");
		order.setDistna("高坪区");
		order.setCityna("南充市");
		order.setProvna("四川省");
		order.setRecena("cc");
		order.setMobile("65223");
		order.setSkuQty(2);
		order.setErpType("OTHERS");
		order.setOrdeno("5666666666811");
		EdiOrder order2 = new EdiOrder();
		order2.setAddres("老君镇1村");
		order2.setDistna("高坪区");
		order2.setCityna("南充市");
		order2.setProvna("四川省");
		order2.setRecena("cc");
		order2.setMobile("65223");
		order2.setSkuQty(2);
		order2.setErpType("TB");
		order2.setOrdeno("5666666666911");
		ShopSource sp = new ShopSource();
		sp.setTopisvSessionKey(sessionKey);
		sp.setTopisvUserId("1");
		TopWaybillNoGetDto waybillDto = new TopWaybillNoGetDto();
		WorkOrder workOrder2 = new WorkOrder();
		workOrder2.setOrder(order2);
		//waybillDto.add(order, sp, "YUNDA");
		waybillDto.add(workOrder2, sp, "YUNDA");
		WlbWaybillICancelResponse cancelResponse = cancelWaybillNo(order2,sp);
		List<WlbWaybillIGetResponse> wlbWaybillGetResponses = topGetWaybillNoBatch(waybillDto);
		for (WlbWaybillIGetResponse wlbWaybillIGetResponse : wlbWaybillGetResponses) {
			if(!StringUtils.isBlank(wlbWaybillIGetResponse.getErrorCode())){
				System.out.println(wlbWaybillIGetResponse.getMsg());
				System.out.println(wlbWaybillIGetResponse.getErrorCode());
				System.out.println(wlbWaybillIGetResponse.getSubCode());
				System.out.println(wlbWaybillIGetResponse.getSubMsg());
			}
			List<WaybillApplyNewInfo> results = wlbWaybillIGetResponse.getWaybillApplyNewCols();
			if(!results.isEmpty()){
				for (WaybillApplyNewInfo waybillApplyNewInfo : results) {
					String waybillNo = waybillApplyNewInfo.getWaybillCode();
					System.out.println(waybillNo);
					String orderNO = waybillApplyNewInfo.getTradeOrderInfo().getPackageId();
					WorkOrder workOrder = getEdiOrder(waybillDto, orderNO);
					workOrder.getOrder().setToShortAddress(waybillApplyNewInfo.getShortAddress());
					workOrder.getOrder().setMailNo(waybillNo);
				}
			}
		}
		List<WlbWaybillIPrintResponse> printCheck = printCheck(waybillDto);
		for (WlbWaybillIPrintResponse wlbWaybillIPrintResponse : printCheck) {
			if(!StringUtils.isBlank(wlbWaybillIPrintResponse.getErrorCode())){
				System.out.println(wlbWaybillIPrintResponse.getMsg());
				System.out.println(wlbWaybillIPrintResponse.getErrorWaybillCode());
				System.out.println(wlbWaybillIPrintResponse.getSubCode());
				System.out.println(wlbWaybillIPrintResponse.getSubMsg());
			}
		}
		System.out.println("===");*/
	}
}
